package data

import (
	"errors"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gin-gonic/gin"
	"path"
	"server/model"
)

func GetFont(language string) (*gin.H, error) {
	var items []model.FontItem
	const FontPath = "/home/sftp_root/shared/FONTSUPPLY"
	background := ""
	direction := ""
	file, err := excelize.OpenFile(path.Join(FontPath, "DF_"+language+".xlsx"))
	if err != nil {
		return nil, err
	}
	rows, err := file.GetRows("Sheet1")
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if len(row) < 3 {
			return nil, errors.New("invalid file")
		}
		if background == "" && len(row) > 3 && row[3] != "" {
			background = row[3]
		}
		if direction == "" && len(row) > 4 && row[4] != "" {
			direction = row[4]
		}
		items = append(items, model.FontItem{File: row[0], Script: row[1], User: row[2]})
	}
	return &gin.H{
		"background": background,
		"direction":  direction,
		"font":       items,
	}, nil
}
